﻿using Android;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using AndroidX.Core.App;

namespace Tester.Platforms.Android;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    //public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    //{
    //    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    //    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    //}
    protected override void OnCreate(Bundle savedInstanceState)
    {
#if ANDROID30_0_OR_GREATER
        //if (!Android.OS.Environment.IsExternalStorageManager)
        //{
        //    Intent intent = new Intent();
        //    intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
        //    Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
        //    intent.SetData(uri);
        //    StartActivity(intent);
        //}
#elif ANDROID
            Intent intent = new Intent();
            intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
            Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
            intent.SetData(uri);
            StartActivity(intent);
#endif
        base.OnCreate(savedInstanceState);
    }
    //protected override void OnCreate(Bundle bundle)
    //{
    //    if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
    //    {
    //        if (!(CheckPermissionGranted(Manifest.Permission.ReadExternalStorage) && !CheckPermissionGranted(Manifest.Permission.WriteExternalStorage)))
    //        {
    //            RequestPermission();
    //        }
    //    }
    //    //LoadApplication(new App());
    //}

    //private void RequestPermission()
    //{
    //    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
    //}

    //public bool CheckPermissionGranted(string Permissions)
    //{
    //    // Check if the permission is already available.
    //    if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
    //    {
    //        return false;
    //    }
    //    else
    //    {
    //        return true;
    //    }
    //}
}
